﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using DTO;
namespace DAO
{
    public class TaiKhoanTheDA
    {
        public static TaiKhoanThe GetByID(int maID)
        {
            TaiKhoanThe tk = null;
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_TaiKhoanThe_GetById", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@maTKT", maID));
            SqlDataReader dataReader = cmd.ExecuteReader();
            if (dataReader.Read())
            {
                tk = new TaiKhoanThe();
                tk.MaTaiKhoanThe = maID;
                tk.MaTKChinh = Convert.ToInt32(dataReader["MaTKChinh"]);
                tk.SoDu = Convert.ToSingle(dataReader["SoDu"]);
                tk.DelFlg = Convert.ToInt32(dataReader["DelFlg"]);
                tk.LaiSuat = Convert.ToSingle(dataReader["LaiSuat"]);
                tk.MaLoaiThe = Convert.ToInt32(dataReader["MaLoaiThe"]);
                tk.MaLoaiTien = Convert.ToInt32(dataReader["MaLoaiTien"]);
                tk.MaLoaiTK = Convert.ToInt32(dataReader["MaLoaiTK"]);
                tk.LockFlg = Convert.ToInt32(dataReader["LockFlg"]);
                tk.TaiKhoanChinh = TaiKhoanChinhDA.GetByID(tk.MaTKChinh);
                tk.LoaiMoney = LoaiTienDA.GetByID(tk.MaLoaiTien);
                tk.LoaiCard = LoaiTheDA.GetByID(tk.MaLoaiThe);
                tk.LoaiTK = LoaiTaiKhoanDA.GetByID(tk.MaLoaiTK);
            }
            cn.Close();
            return tk;
        }
        public static List<TaiKhoanThe> GetByMaTaiKhoanChinh(int maID,int type)
        {
            List<TaiKhoanThe> lst = new List<TaiKhoanThe>();
            TaiKhoanThe tk = null;
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_TaiKhoanThe_GetByMaTaiKhoanChinh", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@maTK", maID));
            cmd.Parameters.Add(new SqlParameter("@type", type));
            SqlDataReader dataReader = cmd.ExecuteReader();
            while (dataReader.Read())
            {
                tk = new TaiKhoanThe();
                tk.MaTKChinh = maID;
                tk.MaTaiKhoanThe = Convert.ToInt32(dataReader["MaTaiKhoanThe"]);
               
                tk.SoDu = Convert.ToSingle(dataReader["SoDu"]);
                tk.DelFlg = Convert.ToInt32(dataReader["DelFlg"]);
                tk.LaiSuat = Convert.ToSingle(dataReader["LaiSuat"]);
                tk.MaLoaiThe = Convert.ToInt32(dataReader["MaLoaiThe"]);
                tk.MaLoaiTien = Convert.ToInt32(dataReader["MaLoaiTien"]);
                tk.MaLoaiTK = Convert.ToInt32(dataReader["MaLoaiTK"]);
                tk.LockFlg = Convert.ToInt32(dataReader["LockFlg"]);
               
                tk.TaiKhoanChinh = TaiKhoanChinhDA.GetByID(tk.MaTKChinh);
                
                tk.LoaiMoney = LoaiTienDA.GetByID(tk.MaLoaiTien);
                tk.LoaiTien = tk.LoaiMoney.TenTien;
                
                tk.LoaiCard = LoaiTheDA.GetByID(tk.MaLoaiThe);
                tk.LoaiThe = tk.LoaiCard.TenLoai;
                
                tk.LoaiTK = LoaiTaiKhoanDA.GetByID(tk.MaLoaiTK);
                tk.LoaiTaiKhoan=tk.LoaiTK.MoTa;
                lst.Add(tk); 
            }
            cn.Close();
            return lst;
        }
        public static List<string> GetByTinhTrang()
        {
            List<string> lst = new List<string>();
            TaiKhoanThe tk = null;
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_TaiKhoanThe_GetByTinhTrang", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dataReader = cmd.ExecuteReader();
            while (dataReader.Read())
            {
                tk = new TaiKhoanThe();
                tk.LockFlg = Convert.ToInt32(dataReader["LockFlg"]);
                string a;
                if (tk.LockFlg == 0)
                    a = "Thẻ đã khóa";
                else
                    a = "Thẻ đang hoạt động";
                lst.Add(a);
            }
            cn.Close();
            return lst;
        }
        public static DataTable TaoTKThe(TaiKhoanThe tk,int maKH,string mapin)
        {
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_TaiKhoanThe_Insert", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@maTKC", tk.MaTKChinh));
            cmd.Parameters.Add(new SqlParameter("@sodu", tk.SoDu));
            cmd.Parameters.Add(new SqlParameter("@laisuat", tk.LaiSuat));
            cmd.Parameters.Add(new SqlParameter("@maloaithe", tk.MaLoaiThe));
            cmd.Parameters.Add(new SqlParameter("@maloaitien", tk.MaLoaiTien));
            cmd.Parameters.Add(new SqlParameter("@maloaiTK", tk.MaLoaiTK));
            cmd.Parameters.Add(new SqlParameter("@maKH", maKH));
            cmd.Parameters.Add(new SqlParameter("@mapin", mapin));
            //cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            cn.Close();
            return dt;
        }
    }
}
